package com.nuanshui.heatedloan.baseservice.service;

import com.alibaba.fastjson.JSONObject;
import com.nuanshui.framework.lang.SysStringUtils;
import com.nuanshui.framework.utils.http.HttpRequestUtils;
import com.nuanshui.heatedloan.baseservice.express.SnCal;
import com.nuanshui.heatedloan.baseservice.facade.BaiduLbsService;
import com.nuanshui.heatedloan.baseservice.vo.MctXY;
import com.nuanshui.heatedloan.constant.ServiceConstant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.Map;

@Service(ServiceConstant.BAIDULBSSERVICE)
public class BaiduLbsServiceImpl implements BaiduLbsService{

	@Value("${lbs.baidu.ak}")
	String ak;
	
	@Value("${lbs.baidu.sk}")
	String sk;
	
	private static String webSite = "http://api.map.baidu.com";
	
//	//坐标转换API
//	private static String transCoordsXY = "/geoconv/v1/?";
	
	//根据坐标获取地理位置
	private static String geocodingApi = "/geocoder/v2/?";
	
	//根据ip获取地理位置
	private static String loactionApi = "/location/ip?";
	
	private final static double PI = 3.14159265358979323; // 圆周率
    private final static double R = 6371229; // 地球的半径

	@Override
	public String queryLocation(MctXY point) {
		String address = "";
		Map<String, String> paramsMap = new LinkedHashMap<String, String>();
		paramsMap.put("coordtype", "wgs84ll");
		paramsMap.put("location", point.getLatitude()+","+point.getLongitude());
		paramsMap.put("output", "json");
		paramsMap.put("ak", ak);
		String sn = SnCal.cal(paramsMap, geocodingApi, sk);
		paramsMap.put("sn", sn);
		String requestUrl = webSite + geocodingApi + SysStringUtils.toQueryString(paramsMap);
		String html = HttpRequestUtils.httpRequest(requestUrl);
		try {
			JSONObject json = JSONObject.parseObject(html);
			if(json != null){
				String status = json.getString("status");
				if(status.equals("0")){
					JSONObject resultJson = json.getJSONObject("result");
					JSONObject addressComponent = resultJson.getJSONObject("addressComponent");
					//{"status":0,"result":{"location":{"lng":120.13937313548,"lat":30.277243447634},"formatted_address":"浙江省杭州市西湖区天目山路147号","business":"西湖,天目山路,黄龙","addressComponent":{"city":"杭州市","district":"西湖区","province":"浙江省","street":"天目山路","street_number":"147号"},"poiRegions":[],"cityCode":179}}
					address = addressComponent.getString("city");
					if(SysStringUtils.isNotBlank(addressComponent.getString("district"))){
						address = address + "-" + addressComponent.getString("district");
					}
				}else{
					address = "未知地点";
//					throw new BizException(GlobalErrorCode.INTERNAL_ERROR, "baidu errorCode :"+status);
				}
			}
		} catch (Exception e) {
			address = "未知地点";
		}
		
		return address;
	}
	
	@Override
	public String queryLocation(String ip){
		String address = "";
		Map<String, String> paramsMap = new LinkedHashMap<String, String>();
		paramsMap.put("ip", ip);
		paramsMap.put("ak", ak);
		String sn = SnCal.cal(paramsMap, loactionApi, sk);
		paramsMap.put("sn", sn);
		
		String requestUrl = webSite + loactionApi + SysStringUtils.toQueryString(paramsMap);
		String html = HttpRequestUtils.httpRequest(requestUrl);
		JSONObject json = JSONObject.parseObject(html);
		if(json != null){
			String status = json.getString("status");
			if(status.equals("0")){
				JSONObject content = json.getJSONObject("content");
				JSONObject address_detail = content.getJSONObject("address_detail");
				//responsehtml: {"address":"CN|浙江|杭州|None|CHINANET|0|0","content":{"address_detail": {"province":"浙江省","city": "杭州市","district":"","street":"","street_number":"","city_code":179},"address":"浙江省杭州市","point":{"y":"3515188.13","x":"13382905.27"}},"status":0}
				address = address_detail.getString("city");
				if(SysStringUtils.isNotBlank(address_detail.getString("district"))){
					address = address + "-" + address_detail.getString("district");
				}
			}else{
				address = "未知地点";
//				throw new BizException(GlobalErrorCode.INTERNAL_ERROR, "baidu errorCode :"+status);
			}
		}
		
		
		return address;
	}
	
	@Override
	public double getDistance(MctXY from, MctXY to) {
		double longt1 = Double.parseDouble(from.getLongitude());
		double lat1 = Double.parseDouble(from.getLatitude());
		double longt2 = Double.parseDouble(to.getLongitude());
		double lat2 = Double.parseDouble(to.getLatitude());
		
        double x, y, distance;
        x = (longt2 - longt1) * PI * R
                * Math.cos(((lat1 + lat2) / 2) * PI / 180) / 180;
        y = (lat2 - lat1) * PI * R / 180;
        distance = Math.hypot(x, y);
        return distance;
    }
}
